<template>
	<view class="page">

		<view class="page-content ">
			<u-tabs :list="list" active-color="#F24646" :is-scroll="false" :current="current" @change="change"></u-tabs>
			<view class="line"></view>
			<view class="p12">
				
				<view v-if="current==1" class="bg-white brs10 p15 mb10">
					
					<view class="u-flex u-f-jsb">
						<view class="fz12 c6 u-f1" style="text-align: center;">
							已投资(套)<view class="fz16 c3 mt8">{{dataInfo.investmentCount || 0}}</view>
						</view>
						<view class="fz12 c6 u-f1" style="text-align: center;">
							待审核(套)<view class="fz16 c3 mt8">{{dataInfo.unInvestmentCount || 0}}</view>
						</view>
						<view @click="toPage('/pages/user/balance/list?type=2')" class="fz12 c6 u-f1" style="text-align: center;">
							累计收益(元)<view class="fz16 c3 mt8">{{dataInfo.incomeAmount || 0}}</view>
						</view>
					</view>
					<view class="u-flex u-f-jsb mt20">
						<view class="fz12 c6 u-f1" style="text-align: center;">
							投资金额(元)<view class="fz16 c3 mt8">{{dataInfo.investmentAmount || 0}}</view>
						</view>
						<view class="fz12 c6 u-f1" style="text-align: center;">
							投资金额（待审）<view class="fz16 c3 mt8">{{dataInfo.unInvestmentAmount || 0}}</view>
						</view>
						<view @click.stop="" class="fz12 c6 u-f1" style="text-align: center;">
							预计每月收入(元)<view class="fz16 c3 mt8">{{dataInfo.preMonthAmount || 0}}</view>
						</view>
					</view>
				</view>
				<view v-for="(item,index) in dataList" :key="index" @click="toEdit(item)">
				
					<view v-if="current==0" class="bg-white mb15 p10 u-flex" style="box-shadow: 0px 8px 20px 0px rgba(230,219,209,0.25);align-items: flex-start;">
						<image :src="item.mainImage" style="width:200rpx;height: 200rpx;min-width:200rpx;" mode="aspectFill"></image>
						<view class="u-f1 ml10">
							
								<view class="fz13 u-line-2">{{item.propertyName}}</view>
								<view class="u-f-jsb u-flex">
									<view class="u-flex fz12 mt10">
										<span class="c6">投资份额</span>
										<span class="color-price ml6">{{item.remainingShares}}</span>
									</view>
									
									<view class="u-flex fz12 mt4" v-if="item.preIncome">
										<span class="c6">预计收益</span>
										<span class="color-price ml6">￥{{item.preIncome}}</span>
									</view>
								</view>
								
									<view class="u-flex fz12 mt5">
										<span class="c6">月租</span>
										<span class="color-price ml6">￥{{item.rentAmount}}</span>
									</view>
									<view class="u-flex fz12 mt5">
										<span class="c6">{{item.isRented==1?'出租中':'未出租'}}</span>
										<span class="color-price ml6" v-if="item.leaseEndDateFormat && item.isRented==1">租期至{{item.leaseEndDateFormat}}</span>
									</view>
								
								<!-- <view class="u-flex fz12 mt4">
								<span class="color-price">查看房源介绍</span>
								</view> -->
								
							
						</view>
					</view>
					<view v-else class="bg-white mb15 p10 u-flex" style="box-shadow: 0px 8px 20px 0px rgba(230,219,209,0.25);align-items: flex-start;">
						<image :src="item.productImage" style="width:200rpx;height: 220rpx;" mode="aspectFill"></image>
						<view class="u-f1 pl10">
							<view class="fz14 u-f-jsb u-flex" style="align-items: flex-start;">{{item.productName}}<span class="status fz12" style="min-width: 100rpx;text-align: right;">{{item.status?'已投资':'待审核'}}</span>
							</view>
							<view class="u-f-jsb u-flex">
								
								<view class="u-flex fz12 mt8">
									<span class="c6">已收益</span>
									<span class="color-price ml6">￥{{item.incomeAmount || 0}}</span>
								</view>
								<view class="u-flex fz12 mt5">
									<span class="c6">租金</span>
									<span class="color-price ml6">￥{{item.rentAmount || 0}}</span>
								</view>
							</view>
							<view class="u-flex fz12 mt8">
								<span class="c6">{{item.isRented}}</span>
								<span class="color-price ml6" v-if="item.leaseEndDate">租期至{{item.leaseEndDate}}</span>
							</view>
							<!-- <view v-if="item.electronicContract" @click.stop="openFiles(item.electronicContract)"></view> -->
							<view class="u-flex fz12 mt12 ">
								<span class="mr15" style="background: #e9e9e9;color: #666;padding: 8px 15px;color: #333;" @click.stop="toPeople(item)">租客信息</span>
								<span class="" style="background: #e9e9e9;color: #666;padding: 8px 15px;color: #333" @click.stop="toMoney(item)">收益明细</span>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="loadStatus"></u-loadmore>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				current: 1,
				statusAttr: ['', '待签到', '已签到', '已取消'],
				list: [{
					name: '可投资',
					id: '',
					count:0
				}, {
					name: '已投资',
					id: '1',
					count:0
				}, ],
				cateList:[],
				loadStatus: 'more',
				scrollHeight: 300,
				dataList: [],
				dataInfo:{},
				page: 1,
			}
		},
		onLoad(e) {
			if (e.tab) {
				this.current = e.tab
			}
			this.$nextTick(() => {
				this.initData()
				
				this.$api.getNumber({}, res => {
					if (res.code == 200) {
						if (res.data) {
				
							this.dataInfo = res.data;
						}
					}
				})
				
				
				this.$api.buyList({}, (res) => {
					if (res.code == 200) {
						this.list[0].count=res.data.total
					}
				})
				this.$api.getAllInvestmentProduct({}, (res) => {
					if (res.code == 200) {
						this.list[1].count=res.data.total
					}
				})
				
			})
		},
		onShow() {
			
		},
		// 页面下拉到底部触发
		onReachBottom() {
			if (this.loadStatus == 'more') {
				this.getData()
			}
		},
		methods: {
			toPage(url){
				uni.navigateTo({
					url
				})
			},
			toPeople(item){
				uni.navigateTo({
					url:'/pages/house/people?id='+item.propertyId
				})
			},
			
			toMoney(item) {
				uni.navigateTo({
					url: '/pages/house/money?type=1&id=' + item.propertyId
				})
			},
			openFiles(url) {
				uni.showLoading({
					title: '下载中...'
				});
				uni.downloadFile({
					url: url, //源文件地址
					success(res) {
						uni.hideLoading()
						uni.openDocument({
							filePath: res.tempFilePath,
							// fileType: "pdf",
							showMenu: true,
							success: function(res) {
								console.log('文件打开成功');
							},
							fail: function(err) {
								console.error('文件打开失败', err);
							}
						})
					},
					complete: () => {

						uni.hideLoading()
					}
				})
			},
			toEdit(item){
				if(this.current==0){
					
					uni.navigateTo({
						url: `/pages/product/info?id=${item.id}`
					})
				}else{
				uni.navigateTo({
					url: `/pages/product/info?id=${item.propertyId}`
				})
					
				}
			},
			change(e) {
				this.current = e
				this.initData()
			},
			initData() {
				this.page = 1
				this.dataList = []
				this.getData()
			},
			
			getData() {
				
				if(this.current==0){
					
					
					this.$api.buyList({
						pageNo: this.page,
						pageSize: 10,
						// status: this.list[this.current].id
					}, res => {
						if (res.code) {
							let _list = res.data.list
							this.dataList = [...this.dataList, ..._list]
							// 判断所有数据是否请求完毕
							if (res.data.total > this.dataList.length) {
								this.page++
								this.loadStatus = 'more'
							} else {
								this.loadStatus = 'noMore'
							}
						}
					})
				}else{
					
					this.$api.getAllInvestmentProduct({
						pageNo: this.page,
						pageSize: 10,
						// status: this.list[this.current].id
					}, res => {
						if (res.code) {
							res.data.list.some(item => {
								if(item.propertyImages){
									this.$set(item,'img',item.propertyImages.split(',')[0])
								}
								this.list.some(item1 => {
									if(item.status==item1.id){
										this.$set(item,'statusDec',item1.name)
									}
									
								})
							})
							let _list = res.data.list
							this.dataList = [...this.dataList, ..._list]
							// 判断所有数据是否请求完毕
							if (res.data.total > this.dataList.length) {
								this.page++
								this.loadStatus = 'more'
							} else {
								this.loadStatus = 'noMore'
							}
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.btn {
		padding: 0 30rpx;
		height: 60rpx;
		text-align: center;
		line-height: 60rpx;
			background: rgba(242, 70, 70, 0.1);
		color: #F24646;
		border-radius: 40px;
		&.btn-del{
			background: #f5f5f5;
			color: #999;
		}
	}

	.line {
		width: 750rpx;
		height: 4rpx;
		position: relative;
		top: -4rpx;
		background: #F6F9F9;
		border-radius: 0px 0px 0px 0px;
	}

	.status1 {
		color: #E54440;
	}

	.status2 {
		color: #39959B;
	}

	.status3 {
		color: #333;
	}
</style>